<?php
/**
 * Purpose: 出行人管理
 * User: peter
 * Date: 2018/9/14
 * Time: 下午2:00
 */

namespace app\activity\logic;


use extend\Functions;
use extend\IDValidator;
use extend\STATUS_CODE;

class TravelPeople
{
    /**
     * @purpose 添加出行人
     * @param array $data
     * @param int $uid
     * @return array
     */
    public static function add(array $data, int $uid): array
    {
        if (!isset($data['name']) || empty($data['name'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少出行人姓名..!'
            );
        }
        if (!isset($data['sex']) ||
            empty($data['sex']) ||
            !in_array($data['sex'], [1, 2])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少出行人性别..!'
            );
        }
        if (!isset($data['type']) ||
            empty($data['type']) ||
            !in_array($data['type'], [1, 2])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少出行人类型...!'
            );
        }
        if (!isset($data['mobile']) || empty($data['mobile'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少出行人手机号码...!'
            );
        }
        if (!Functions::check_mobile($data['mobile'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '手机号码格式错误...!'
            );
        }
        if (!isset($data['certificate_type']) ||
            empty($data['certificate_type']) ||
            !in_array($data['certificate_type'], [1, 2, 3, 4, 5])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少证件类型...!'
            );
        }
        if (!isset($data['certificate_number']) ||
            empty($data['certificate_number'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少证件号码...!'
            );
        }
        if ($data['certificate_type'] === 1) {
            $validate = IDValidator::getInstance();
            if (!$validate->isValid($data['certificate_number'])) {
                return Functions::result(
                    STATUS_CODE::PARAMETER_ERROR,
                    '无效的身份证号码'
                );
            }
        }
        $_data                       = [];
        $_data['uid']                = $uid;
        $_data['name']               = trim($data['name']);
        $_data['sex']                = trim($data['sex']);
        $_data['type']               = trim($data['type']);
        $_data['mobile']             = trim($data['mobile']);
        $_data['certificate_type']   = trim($data['certificate_type']);
        $_data['certificate_number'] = trim($data['certificate_number']);
        $model                 =    new \app\model\TravelPeople();
        $result                = $model
            ->isUpdate(false)
            ->data($_data)
            ->save();
        if ($result){
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '添加成功!'
            );
        }
        return Functions::result(
            STATUS_CODE::CREATE_FAIL,
            '添加失败!'
        );
    }

    /**
     * @purpose 编辑出行人
     * @param array $data 必须包含ID字段
     * @param int $uid
     * @return array
     */
    public static function edit(array $data): array
    {
        if (!$data['id']) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '参数错误!'
            );
        }
        if (!isset($data['name']) || empty($data['name'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少出行人姓名..!'
            );
        }
        if (!isset($data['sex']) ||
            empty($data['sex']) ||
            !in_array($data['sex'], [1, 2])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少出行人性别..!'
            );
        }
        if (!isset($data['type']) ||
            empty($data['type']) ||
            !in_array($data['sex'], [1, 2])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少出行人类型...!'
            );
        }
        if (!isset($data['mobile']) || empty($data['mobile'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少出行人手机号码...!'
            );
        }
        if (!Functions::check_mobile($data['mobile'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '手机号码格式错误...!'
            );
        }
        if (!isset($data['certificate_type']) ||
            empty($data['certificate_type']) ||
            !in_array($data['certificate_type'], [1, 2, 3, 4, 5])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少证件类型...!'
            );
        }
        if (!isset($data['certificate_number']) ||
            empty($data['certificate_number'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少证件号码...!'
            );
        }
        if ($data['certificate_type'] === 1) {
            $validate = IDValidator::getInstance();
            if (!$validate->isValid($data['certificate_number'])) {
                return Functions::result(
                    STATUS_CODE::PARAMETER_ERROR,
                    '无效的身份证号码'
                );
            }
        }
        $model          = new \app\model\TravelPeople();
        $result         = $model
            ->allowField(true)
            ->save($data, ['id' => $data['id']]);
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '修改成功'
            );
        }
        return Functions::result(
            STATUS_CODE::UPDATE_FAIL,
            '修改失败!'
        );
    }

    /**
     * @purpose 删除出行人, 支持批量删除
     * @param string $ids 示例批量删除:[1,2,3,4,5]
     * @return array
     * @throws \Exception
     */
    public static function del(array $ids = [], int $uid): array
    {
        if (empty($ids)) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '参数错误!'
            );
        }
        $model          = new \app\model\TravelPeople();
        $result         = $model
            ->where('uid', $uid)
            ->where(['id' => $ids])
            ->delete();
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '删除成功!'
            );
        }
        return Functions::result(
            STATUS_CODE::DELETE_FAIL,
            '删除失败!'
        );
    }

    /**
     * @purpose 获取出行人列表
     * @param array $where
     * @param int $page
     * @param int $size
     * @param int $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function items(array $where = [], int $page = 1, int $size = 50, int $uid): array
    {
        $model          = new \app\model\TravelPeople();
        $list          = $model
            ->where($where)
            ->where('uid', $uid)
            ->field('id,name,sex,type,mobile,certificate_type,certificate_number')
            ->page($page,$size)
            ->order('create_at desc')
            ->select();
        $total          = $model
            ->where($where)
            ->where('uid', $uid)
            ->count(1);
        return Functions::result(
            STATUS_CODE::SUCCESS,
            [
                'total' => $total,
                'list' => $list
            ]
        );
    }

    /**
     * @purpose 通过ID获取单条出行人信息
     * @param int $id
     * @param int $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function infoById (int $id, int $uid): array
    {
        if (!$id) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '参数错误!'
            );
        }
        $model          = new \app\model\TravelPeople();
        $result         = $model
            ->field('id,name,sex,type,mobile,certificate_type,certificate_number')
            ->where('id', $id)
            ->where('uid', $uid)
            ->find();
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $result
        );
    }
}